//
//  UIImage+Mix.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/29.
//
//

#import "UIImage+Mix.h"

@implementation UIImage (Mix)

+ (UIImage *)mixImageWithFrontImage:(UIImage *)front andBackImage:(UIImage *)back{
    
    if (front == nil) {
        NSLog(@"front");
    }else if (back == nil){
        NSLog(@"back");
    }else{
        NSLog(@"all exist");
    }
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
//    CGImageRef backRef = back.CGImage;
    CGFloat width = 1598;////CGImageGetWidth(backRef);
    CGFloat height = 973;//CGImageGetHeight(backRef);
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [back drawInRect:CGRectMake(0, 0, width, height)];
    
    CGFloat frontTop = 19.3*3;
    CGFloat frontHeight = height - 65*3;
    CGFloat frontWidth = front.size.width / front.size.height * frontHeight;
    CGFloat frontLeft = (width - frontWidth)/2;
    
    [front drawInRect:CGRectMake(frontLeft, frontTop, frontWidth, frontHeight)];
    
    UIImage *medialink = kImgFromFile(Babel(@"bottom_medialink"), @"png");
    UIImage *tlp = kImgFromFile(@"bottom_oxfam_tlp", @"png");
    [medialink drawInRect:CGRectMake(18.3*3, 292.3*3, 210*3, 23.3*3)];
    [tlp drawInRect:CGRectMake(290.3*3, 287*3, 224*3, 31.7*3)];
    
    UIImage *left = kImgFromFile(@"bottom_left", @"png");
    UIImage *right = kImgFromFile(Babel(@"bottom_right"), @"png");
    [left drawInRect:CGRectMake(0, 0, 0, 0)];
    [right drawInRect:CGRectMake(0, 0, 0, 0)];

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
//    CGImageRelease(backRef);
    return result;
}

@end
